/*
	Figure_01 PD and orthogonalized sentiment
	Figure_02 Panel VAR
	Figure_03 Consumer sentiment and weather patterns
*/

* Figure_01. PD and orthogonalized sentiment
	clear all
	use "data/final_data_set.dta"
	xtset id year

	/* countries
		G7: Canada, France, Germany, Italy, United Kingdom, and United States
		Non-G7: Australia, Austria, Belgium, Denmark, Finland, Ireland, Netherlands, New Zealand, Spain, Sweden, and Switzerland
	*/

	// simple correlations
		pwcorr zpd zcci_ort, sig
		pwcorr zpd zcci_exp, sig
			
	// AUS AUT BEL CAN DNK FIN FRA DEU IRL ITA NLD NZL ESP SWE CHE GBR USA 
		la var zcci_ort "S"
		la var zpd "PD"
		foreach x in AUS AUT BEL CAN DNK FIN FRA DEU IRL ITA NLD NZL ESP SWE CHE GBR USA {
		qui: twoway ///
			(tsline zpd, yaxis(1) lcolor(blue)) ///
			(tsline zcci_ort, lcolor(red)) ///
			if cou == "`x'" & year > 1974, caption("") note("") ylabel(none) xtitle("`x'") tlabel(1975 1985 1995 2005 2015) graphregion(color(white))
			graph save `x', replace
			}
		gr combine AUS.gph AUT.gph BEL.gph CAN.gph DNK.gph FIN.gph FRA.gph DEU.gph IRL.gph, ysize(6)
		graph export "pd_sentiment_1.png", as(png) replace
		gr combine ITA.gph NLD.gph NZL.gph ESP.gph SWE.gph CHE.gph GBR.gph USA.gph, ysize(6)
		graph export "pd_sentiment_2.png", as(png) replace
			
		foreach x in AUS AUT BEL CAN DNK FIN FRA DEU IRL ITA NLD NZL ESP SWE CHE GBR USA {
		erase `x'.gph
		}
		
	/* Alternative: groups of three
		la var zcci_ort "S"
		la var zpd "PD"
		foreach x in AUS AUT BEL CAN DNK FIN FRA DEU IRL ITA NLD NZL ESP SWE CHE GBR USA {
		qui: twoway ///
			(tsline zpd, yaxis(1) lcolor(blue)) ///
			(tsline zcci_ort, lcolor(red)) ///
			if cou == "`x'" & year > 1974, caption("") note("") xtitle("`x'") tlabel(1975 1985 1995 2005 2015) graphregion(color(white))
			graph save `x', replace
			}
			
		gr combine AUS.gph AUT.gph BEL.gph, cols(3) xsize(10)
		graph export "pd_non_G7_1.png", as(png) replace
			
		foreach x in AUS AUT BEL CAN DNK FIN FRA DEU IRL ITA NLD NZL ESP SWE CHE GBR USA {
		erase `x'.gph
		}	
		*/
		
		
* Figure_02. Panel VAR
	/* Estimation of panel vector autoregression in Stata: https://journals.sagepub.com/doi/pdf/10.1177/1536867X1601600314
		// help file
			help pvar
			help pvarirf
		// useful routines
			xtdescribe
			pvargranger
			pvarstable
			pvarirf, tab
			pvarirf, tab mc(200)
			pvarirf, tab oirf mc(200)
		// first-differences
			pvar si b1, lags(1) fd
	*/
		
	clear all
	use "data/final_data_set.dta"
	tsset id year
	sort id year
	set more off

	// standardized productivity
		foreach x in rtfpna {
		egen `x'mean = mean(`x')
		egen `x'sd = sd(`x')
		gen z`x' = (`x'-`x'mean) / `x'sd
		drop `x'sd `x'mean
		}
		
	rename zcci Sentiment
	rename zcci_exp Explained
	rename zcci_ort Unexplained
	rename zrtfpna Productivity
	
	// Productivity and sentiment	
		foreach y in Sentiment {
		foreach x in Productivity { // zcci zcci_ort zcci_exp
		forvalues num = 4/4 { // 1 to 4
			pvar `y' `x', lags(`num') fod
			pvarirf, oirf mc(200) byoption(yrescale) porder(`x' `y')
			graph export "Figure_01_IRF_`y'_`x'_fod.png", as(png) replace
		}
	}
	}
		

* Figure_03. Consumer sentiment and weather patterns
	clear all
	use "data/final_data_set.dta"
	xtset id year

	merge m:m cou year using "data/weather.dta"
	drop _merge

	duplicates drop id year, force
	xtset id year

	// weather variables: DP01 DP10 DP1X PRCP DP01_i DP10_i DP1X_i PRCP_i

	// generate precipitation variable that excludes extreme rain days
		gen PRCP1 = PRCP - DP1X

	// generate rates of growth and dummies
		foreach x in DP01 DP10 DP1X PRCP PRCP1 DP01_i DP10_i DP1X_i PRCP_i {
			gen g_`x' = ln(`x')-ln(l.`x')
			gen dum_`x' = (d.`x' < 0)
			* replace dum_`x' = . if d.`x' == . // dubious command, creates issues below (missing obs)
			replace dum_`x' = . if pd == .
		}

	la var dum_PRCP "D"
	la var zcci "S"

	// AUS AUT BEL CAN DNK FIN FRA DEU IRL ITA NLD NZL ESP SWE CHE GBR USA
		foreach x in AUS AUT BEL CAN DNK FIN FRA DEU IRL ITA NLD NZL ESP SWE CHE GBR USA {
		qui: twoway ///
			(bar dum_PRCP year, yaxis(2) color(gs14)) ///
			(tsline zcci, yaxis(1) lcolor(red)) ///
			if cou == "`x'" & year > 1974, caption("") note("") xtitle("`x'") tlabel(1975 1985 1995 2005 2015) graphregion(color(white))
			graph save `x', replace
			}
			
		gr combine AUS.gph AUT.gph BEL.gph CAN.gph DNK.gph FIN.gph FRA.gph DEU.gph IRL.gph, ysize(4)
		graph export "weather_sentiment_1.png", as(png) replace
		gr combine ITA.gph NLD.gph NZL.gph ESP.gph SWE.gph CHE.gph GBR.gph USA.gph, ysize(4)
		graph export "weather_sentiment_2.png", as(png) replace
			
		foreach x in AUS AUT BEL CAN DNK FIN FRA DEU IRL ITA NLD NZL ESP SWE CHE GBR USA {
		erase `x'.gph
		}

	